﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ys000;

namespace OcmKeep
{
    public partial class FormPreview : Form
    {
        System.Collections.Generic.List<PictureBox> previewPb = Form1.hostPb;

        public FormPreview()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.Load+=FormPreview_Load;
            this.Resize += FormPreview_Resize;
        }

        void FormPreview_Load(object sender, EventArgs e)
        {
            CreateUI();
            //new Thread(new ThreadStart(CreateUI)).Start();
            
        }

        private void CreateUI()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate { CreateUI(); }));
                return;
            }
            int faceNum = previewPb.Count;
            Size facesize = new Size();
            //this.Controls.Clear();
            if (faceNum == 1)
            {
                this.Dock = DockStyle.Fill;
            }
            else if (faceNum == 2)
            {
                VideoResize();
                for (int i = 0; i < faceNum; ++i)
                {
                    this.Controls.Add(previewPb[i]);
                }

            }
        }

        private void VideoResize() {
            int faceNum = previewPb.Count;
            int w = (this.Width - 20) / 2;
            Size facesize;
            int h = this.Height - 20;
            if (w < h) facesize = new Size(w, w);
            else facesize = new Size(h, h);
            for (int i = 0; i < faceNum; ++i)
            {
                previewPb[i].Size = facesize;
                previewPb[i].Location = new Point(i * (facesize.Width + 5)+5, 10);
            }
        }

        void FormPreview_Resize(object sender, EventArgs e)
        {
            VideoResize();
        }
    }

}
